package com.enhinck.dbutilweb.common.freemark.mybatisplus;

import com.enhinck.dbutilweb.common.excel.JavaDefineEntity;
import com.enhinck.dbutilweb.common.excel.JavaFieldEntity;
import com.enhinck.dbutilweb.common.freemark.BaseFacotry;
import com.enhinck.dbutilweb.common.freemark.FreemarkUtil;
import com.enhinck.dbutilweb.common.freemark.JavaClassEntity;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * VO工厂
 *
 * @author huenbin
 * @date 2020-07-02 17:00
 */
public class VOFactory extends BaseFacotry {
    private static final VOFactory doFactory = new VOFactory();

    public static VOFactory getInstance() {
        return doFactory;
    }

    private VOFactory() {

    }


    @Override
    public String parentClass() {
//        if (isIoc) {
//            return "BaseVO";
//        }
        return null;
    }


    public JavaClassEntity create(JavaDefineEntity<JavaFieldEntity> javaDefineEntity) {

        return create(javaDefineEntity,null);
    }


    public JavaClassEntity create(JavaDefineEntity<JavaFieldEntity> javaDefineEntity, String javaClassName) {
        JavaClassEntity javaClassEntity = createClass(javaDefineEntity);
        String className = getVOName(javaDefineEntity);
        if (StringUtils.isNotBlank(javaClassName)) {
            className = javaClassName;
        }
        javaClassEntity.setClassName(className);

        List<String> annotations = commonVODTOAnnotations(javaDefineEntity, "VO");
        javaClassEntity.setAnnotations(annotations);

        Set<String> importList = commonDTOVOImports();
        importList.add("java.io.Serializable");
        if (isIoc) {
            //  importList.add("com.greentown.common.model.vo.BaseVO");
        }
        importList.add("com.alibaba.fastjson.annotation.JSONField");

        List<String> implementsInterfaces = new ArrayList<>();
        implementsInterfaces.add("Serializable");
        javaClassEntity.setImplementsInterfaces(implementsInterfaces);
        javaClassEntity.setImportList(importList);

        javaClassEntity.setPackagePath(javaDefineEntity.getVOPackageName());


        processField(javaDefineEntity, javaClassEntity, true);


        return javaClassEntity;
    }

    public void write(JavaDefineEntity<JavaFieldEntity> javaDefineEntity) {
        FreemarkUtil.write(create(javaDefineEntity));
    }
}
